import {
  Column,
  CreateDateColumn,
  Entity,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import Decimal from 'decimal.js';
import { Good } from '../../good/entities/good.entity';
import { imgDto } from '../../dto/all-dto';

@Entity()
export class Sku {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    comment:'规格组合',
    type:'json'
  })
  specCombination: Record<string | number,string>

  @Column({
    comment:'图片',
    nullable: true,
    type:"json"
  })
  img: imgDto;

  @Column({
    comment:"sku 售价",
    type:'decimal',
    scale: 2,
    default: 0.00,
    transformer: {
      from: (value: string | null) => (value !== null ? new Decimal(value).toNumber() : 0), // 将字符串转换为数字
      to: (value: number | null) => (value !== null ? value.toFixed(2) : '0.00'), // 将数字转换为数据库格式
    }
  })
  price: number

  @Column({
    comment:'库存数量',
    type:'int',
    default:0
  })
  stock: number

  @ManyToOne(()=> Good,(good)=> good.sku,{nullable:true})
  good: Good

  @CreateDateColumn()
  createTime: Date

  @UpdateDateColumn()
  updateTime: Date;


}
